package com.ssy.lingxi.order.serviceimpl.base;

import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.order.model.constant.OrderServiceContants;
import com.ssy.lingxi.order.model.dto.CountryAreaDTO;
import com.ssy.lingxi.order.model.vo.basic.response.AreaVO;
import com.ssy.lingxi.order.repository.CountryAreaRepository;
import com.ssy.lingxi.order.service.base.IBaseCountryAreaService;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 省市区基础服务接口实现类
 * @author 万宁
 * @version 2.0.0
 * @date 2021-07-17
 */
@Service
public class BaseCountryAreaServiceImpl implements IBaseCountryAreaService {
    @Resource
    private CountryAreaRepository countryAreaRepository;

    /**
     * 查询省列表
     *
     * @param includeCountry 是否包括“全国”
     * @param topItemName    需要插入到列表第0项的名称，空或Null不插入
     * @return 省列表
     */
    @Override
    public Wrapper<List<AreaVO>> listProvinces(boolean includeCountry, String topItemName) {
        List<CountryAreaDTO> dtoList = countryAreaRepository.findByLevel(OrderServiceContants.PROVINCE_LEVEL, Sort.by("code").ascending(), CountryAreaDTO.class);
        List<AreaVO> provinceList = dtoList.stream().map(area -> new AreaVO(area.getCode(), area.getName())).collect(Collectors.toList());
        if(includeCountry) {
            provinceList.add(0, new AreaVO(OrderServiceContants.COUNTRY_CODE, OrderServiceContants.COUNTRY_NAME));
        }

        if(StringUtils.hasLength(topItemName)) {
            provinceList.add(0, new AreaVO("", topItemName));
        }

        return Wrapper.success(provinceList);
    }

    /**
     * 根据省编码查询市列表
     *
     * @param provinceCode 省编码
     * @param topItemName  需要插入到列表第0项的名称，空或Null不插入
     * @return 市列表
     */
    @Override
    public Wrapper<List<AreaVO>> listCities(String provinceCode, String topItemName) {
        List<CountryAreaDTO> dtoList = countryAreaRepository.findByParentCode(provinceCode, Sort.by("code").ascending(), CountryAreaDTO.class);
        List<AreaVO> cityList = dtoList.stream().map(area -> new AreaVO(area.getCode(), area.getName())).collect(Collectors.toList());
        if(StringUtils.hasLength(topItemName)) {
            cityList.add(0, new AreaVO("", topItemName));
        }

        return Wrapper.success(cityList);
    }

    /**
     * 根据市编码查询区列表
     *
     * @param cityCode    市编码
     * @param topItemName 需要插入到列表第0项的名称，空或Null不插入
     * @return 区列表
     */
    @Override
    public Wrapper<List<AreaVO>> listDistrict(String cityCode, String topItemName) {
        List<CountryAreaDTO> dtoList = countryAreaRepository.findByParentCode(cityCode, Sort.by("code").ascending(), CountryAreaDTO.class);
        List<AreaVO> districtList = dtoList.stream().map(area -> new AreaVO(area.getCode(), area.getName())).collect(Collectors.toList());
        if(StringUtils.hasLength(topItemName)) {
            districtList.add(0, new AreaVO("", topItemName));
        }

        return Wrapper.success(districtList);
    }
}
